<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>

        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin-top: 10px">
        <el-table v-loading="loading" border :data="list">
          <el-table-column label="序号" type="index" sortable="" />
          <el-table-column label="姓名" prop="username" sortable="" />
          <el-table-column label="手机号" prop="mobile" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <el-table-column
            label="聘用形式"
            prop="formOfEmployment"
            :formatter="formatEmployment"
            sortable=""
          />
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable="">
            <template #default="{ row }">
              {{ row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{ row }">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button
                type="text"
                size="small"
                @click="del(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            :total="total"
            :current-page="page"
            :page-size="pagesize"
            layout="total,prev, pager, next"
            @current-change="handleCurrentChange"
          />
        </div>

        <!-- 弹出框 -->
        <AddEmployee :show-dialog.sync="showDialog" />
      </el-card>
    </div>
  </div>
</template>
<script>
import AddEmployee from './components/add-employee.vue'
import EmployeeEnum from '@/api/constant/employees'
import { getEmployeeList, delEmployee } from '@/api/employees'
export default {
  name: 'Employees',
  components: {
    AddEmployee
  },
  data() {
    return {
      list: [],
      page: 1, // 当前页
      pagesize: 10, // 每页条数
      total: 0, // 总数
      loading: false,
      showDialog: false
    }
  },
  created() {
    this.getEmployee()
  },
  methods: {
    // 获取员工信息
    async getEmployee() {
      this.loading = true
      const { data: res } = await getEmployeeList(this.page, this.pagesize)
      // console.log(res)
      this.total = res.total
      this.list = res.rows
      this.loading = false
    },
    // 分页处理
    handleCurrentChange(newPage) {
      this.page = newPage
      this.getEmployee()
    },
    // 处理枚举(格式化聘用形式)
    formatEmployment(row, column, cellValue, index) {
      const obj = EmployeeEnum.hireType.find((item) => item.id === cellValue)
      return obj ? obj.value : '未知'
    },
    // 删除员工
    async del(id) {
      const res = await this.$confirm('确定删除吗', '提示').catch((err) => err)
      if (res === 'cancel') return
      await delEmployee(id)
      if ((this.list.length = 1 && this.page > 1)) {
        this.page--
      }
      this.$message.success('删除成功')
      this.getEmployee()
    }
  }
}
</script>
<style>
</style>
